//
//  WMEMyBaseViewController.m
//  WMEMusic
//
//  Created by 恩叔 on 16/5/7.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEMyBaseViewController.h"
#import "WMEReckonClearData.h"
#import "MyDownLoadView.h"
@interface WMEMyBaseViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation WMEMyBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WS(ws);
    // Do any additional setup after loading the view.
    self.title = @"我的";
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
    }];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if(indexPath.row == 0){
        cell.textLabel.text = @"我的下载(*.*)";
    }else if (indexPath.row == 1){
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        CGFloat h = [WMEReckonClearData folderSizeAtPath:path];
        cell.textLabel.text = [NSString stringWithFormat:@"   清除缓存       %.2fMB", h];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.tag = 10000;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }else{
        cell.textLabel.text = @"未定义";
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MyDownLoadView *downLoad = [[MyDownLoadView alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:downLoad animated:YES];
    }
    if (indexPath.row == 1) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        // 提示控制器
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清空缓存么 亲" message:@"清除缓存会导致下次运行程序的时候拖慢 确认删除么" preferredStyle:UIAlertControllerStyleAlert];
        
        
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击确认");
            [WMEReckonClearData clearCache:path];
            UITableViewCell *cell = [self.view viewWithTag:10000];
            CGFloat h = [WMEReckonClearData folderSizeAtPath:path];
            cell.textLabel.text = [NSString stringWithFormat:@"   清除缓存       %.2fMB", h];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
